package denoise;

import java.awt.image.*;

public class ExampleAgent implements DenoiseAgent
{
    // DenoiseAgent must have an empty constructor. (That constructor will be called by grader.)
    public ExampleAgent()
    {
    }

    public BufferedImage denoise(BufferedImage in, BufferedImage [] training)
    {
	//This code looks at the neighboring set of pixels (within a rectangle of dimensions 3*3 
	//surrounding the pixel at (x,y) and counts the number of zeros and ones and sets the pixel at (x,y) to 
        //white if 65% of the neighboring pixels are white else sets it to black. 
	    int width = in.getWidth(), height = in.getHeight();

        BufferedImage out = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        int range = 1;

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {

                int n0 = 0;
                int n1 = 1;

                for (int dy = -range; dy <= range; dy++) {
                    for (int dx = -range; dx <= range; dx++) {

                        int x0 = x + dx;
                        int y0 = y + dy;

                        if (x0 >= 0 && x0 < width && y0 >= 0 && y0 < height) {
                            int v = in.getRGB(x0, y0);
                            if ((v & 0xff) >= 0x80)
                                n1 ++;
                            else
                                n0 ++;
                        }
                    }
                }

                double frac1 = ((double) n1) / (n0 + n1);

                if (frac1 > 0.65)
                    out.setRGB(x, y, 0xffffff);
                else
                    out.setRGB(x, y, 0x000000);
            }
        }

        return out;
    }
}
